package com.xypw.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public static String getDateInFormat(String format) {
		SimpleDateFormat df = new SimpleDateFormat(format);// 设置日期格式
		return df.format(new Date());
	}
	/**
	 * @author 王 建
	 * @param format 指定日期格式;如：yyyy-MM-dd
	 * @param monthNum 根据当前月份的偏移量,0为当前月，1为下一月,-1为上一月
	 * */
	public static String getDateInFormat(String format,int monthNum){
		SimpleDateFormat df = new SimpleDateFormat(format);
		Calendar cal=Calendar.getInstance();
		cal.add(cal.MONTH, monthNum);
		return df.format(cal.getTime());
	}
	
	public static String getDate() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		return df.format(new Date());
	}

	public static String getYear() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy");// 设置日期格式
		return df.format(new Date());
	}

	public static String getHour() {
		SimpleDateFormat df = new SimpleDateFormat("HH");// 设置日期格式
		return df.format(new Date());
	}

	//取得明年的时间
	public static String getNextYear() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.YEAR, 1);
		str = sdf.format(lastDate.getTime());
		return str;
	}

	//取得下个月的时间
	public static String getNextMonth() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("MM");
		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.MONTH, 1);
		str = sdf.format(lastDate.getTime());
		return str;
	}

	//取得下个月的时间
	public static String getNextMonthWithYear() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.MONTH, 1);
		str = sdf.format(lastDate.getTime());
		return str;
	}
	
	/**
	 * 
	 * @Title: getPassedDate
	 * @Author 汪轶
	 * @Description: 取得当前时间的将来某天或过去某天的日期
	 * @param passedDate
	 *            偏移时间量，正为将来的某天，负为过去的某天
	 * @return String 返回格式为 yyyy-MM-dd 的日期
	 */
	public static String getPassedDate(int passedDate) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, passedDate);
		String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal
				.getTime());
		return yesterday;
	}
	/**
	 * 返回天数
	 * @author 王 建
	 * @param format 指定日期格式;
	 * @param _date1 日期字符串
	 * @param _date2 日期字符串
	 * */
	public static long getDays(String format,String _date1,String _date2){
		SimpleDateFormat simp=new SimpleDateFormat(format);
		Date date1=null;
		Date date2=null;
		try {
			date1 = simp.parse(_date1);
			date2= simp.parse(_date2);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long days=(date1.getTime()-date2.getTime())/1000/60/60/24;
		return days;
	}
	
	/**返回当前时间毫秒数*/
	public static long getCurrMillisecond(){
		
		return new Date().getTime();
	}
	/**返回指定时间毫秒数*/
	public static long getAppointMillisecond(String format,String _date1){
		SimpleDateFormat simp=new SimpleDateFormat(format);
		Date date1=null;
		try {
			date1 = simp.parse(_date1);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date1.getTime();
	}
	public static void main(String[] args) {
//		System.out.println(DateUtil.getNextYear());
//		System.out.println(DateUtil.getNextMonth());
//		System.out.println(DateUtil.getNextMonthWithYear());
//		System.out.println(getDateInFormat("yyyyMMdd", -1));
	}
}
